<?php

/* VIP订单 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class ViporderController extends AdminBaseController
{
    var $status=array("0"=>"未支付","1"=>"已完成");
    var $type=array("1"=>"支付宝","2"=>"微信","3"=>"苹果支付","4"=>"Paypal支付","5"=>"支付宝H5支付");
    var $ambient=array(
            "1"=>array(
                '0'=>'App',
                '1'=>'PC',
            ),
            "2"=>array(
                '0'=>'App',
                '1'=>'公众号',
                '2'=>'PC',
            ),
            "3"=>array(
                '0'=>'沙盒',
                '1'=>'生产',
            ),
			"4"=>array(
                '0'=>'沙盒',
                '1'=>'生产',
            ),
			"5"=>array(
                '0'=>'App',
                '1'=>'PC',
            ),
        );
        
    public function index()
    {
        
        $data = $this->request->param();
        $map=[];		
		$status=isset($data['status']) ? $data['status']: '';
        if($status!=''){
            $map[]=['status','=',$status];
        }

        $start_time=isset($data['start_time']) ? $data['start_time']: '';
        $end_time=isset($data['end_time']) ? $data['end_time']: '';
        if($start_time!=""){
           $map[]=['addtime','>=',strtotime($start_time)];
        }
        if($end_time!=""){
           $map[]=['addtime','<=',strtotime($end_time) + 60*60*24];
        }
		

        $uid=isset($data['uid']) ? $data['uid']: '';
        if($uid!=''){
            $map[]=['uid','=',$uid];
        }
		
		$keyword=isset($data['keyword']) ? $data['keyword']: '';
        if($keyword!=''){
            $map[]=['orderno|trade_no','like',"%".$keyword."%"];
        }
        


        
        $list = Db::name('vip_order')
            ->where($map)
            ->order("id desc")
            ->paginate(20);
        $list->each(function($v,$k){
            $v['userinfo']= getUserInfo($v['uid']);
           
            $length=$v['length']/(60*60*24*30);
            
            $v['length']=$length.'个月';
            return $v; 
        });
        $list->appends($data);
        
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);
        $this->assign('status', $this->status);
        $this->assign('type', $this->type);
        $this->assign('ambient', $this->ambient);

        return $this->fetch();
    }


    public function setPay()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        if(!$id){
            $this->error("数据传入失败！");
        }
        
        $orderinfo=DB::name("vip_order")->where("id={$id}")->find();				
        if($orderinfo){
            if($orderinfo['status']==1){
                $this->error("该订单已支付成功");
            }
            /* 更新会员虚拟币 */
            $length=$orderinfo['length'];
            $addtime=time();
            
            $vip=Db::name('vip_user')->where("uid={$orderinfo['uid']}")->find();
            if(!$vip){
                /* 未购买过 */
                $data=[
                    'uid'=>$orderinfo['uid'],
                    'addtime'=>$addtime,
                    'endtime'=>$addtime + $length,
                ];
                Db::name('vip_user')->insert($data);
            }else if($vip['endtime']< $addtime){
                /* 购买-已过期 */
                $data=[
                    'addtime'=>$addtime,
                    'endtime'=>$addtime + $length,
                ];
                Db::name('vip_user')->where("uid={$orderinfo['uid']}")->update($data);
            }else{
                /* 续费 */
                $data=[
                    'addtime'=>$addtime,
                    'endtime'=>$vip['endtime'] + $length,
                ];
                Db::name('vip_user')->where("uid={$orderinfo['uid']}")->update($data);
            }
            
            /* 更新 订单状态 */
            Db::name('vip_order')->where("id='{$orderinfo['id']}'")->update(array("status"=>1));
                

            $this->success('操作成功');
         }else{
            $this->error('数据传入失败！');
         }	
             
    }


}